What is get-stdin?
The get-stdin package is a simple utility that allows you to easily read stdin as a string or buffer in Node.js. It's useful for command-line tools that need to process input piped to them from the command line.
What are get-stdin's main functionalities?
Read stdin as a string
This feature allows you to read the standard input (stdin) as a string. It returns a promise that resolves with the entire input when the stdin stream ends.
const getStdin = require('get-stdin');
getStdin().then(input => {
console.log(input);
});
Read stdin as a buffer
This feature allows you to read the standard input (stdin) as a buffer. It returns a promise that resolves with the entire input as a buffer when the stdin stream ends.
const getStdin = require('get-stdin');
getStdin.buffer().then(input => {
console.log(input);
});
Other packages similar to get-stdin
raw-body
The raw-body package is similar to get-stdin but is more focused on parsing HTTP request bodies and provides more options for limiting size, setting character encoding, and handling errors.
concat-stream
Concat-stream is a writable stream that concatenates all the data from a stream and calls a callback with the result. It can be used in a similar way to get-stdin for collecting stream data, but it is not limited to stdin and does not return a promise.
get-stream
Get-stream is a utility that turns a stream into a string or buffer. It is very similar to get-stdin but works with any stream, not just stdin, and offers more options for handling the stream data.
get-stdin
Get stdin as a string or buffer
Install
$ npm install get-stdin
Usage
const getStdin = require('get-stdin');
getStdin().then(str => {
console.log(str);
});
$ echo unicorns | node example.js
unicorns
API
Both methods returns a promise that is resolved when the end
event fires on the stdin
stream, indicating that there is no more data to be read.
getStdin()
Get stdin
as a string
.
In a TTY context, a promise that resolves to an empty string is returned.
getStdin.buffer()
Get stdin
as a Buffer
.
In a TTY context, a promise that resolves to an empty buffer is returned.
Related
License
MIT © Sindre Sorhus